//
//  AreaViewController.m
//  Funny
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 King. All rights reserved.
//

#import "AreaViewController.h"

@interface AreaViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSDictionary *dic;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) MyScrollerView *picScrollView;
@property (nonatomic, strong) NSMutableArray *photoArr;
@property (nonatomic, strong) UIView *buttonView;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation AreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global, ^{
        [self handle];
        
    });
    [self.view addSubview:self.scrollView];
    UIImageView *backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    backImg.image = [UIImage imageNamed:@"back1.png"];
    
    UIButton *backClick = [UIButton buttonWithFrame:CGRectMake(0, 23, 60, 30) color:nil title:@" 返回" type:UIButtonTypeSystem target:self action:@selector(backToPage) event:UIControlEventTouchUpInside cornerRadius:0];
    backClick.tintColor = [UIColor whiteColor];
    [backClick setImage:[UIImage imageNamed:@"btn_back@3x.png"] forState:UIControlStateNormal];
    
    [self.scrollView addSubview:self.picScrollView];
    [self.view addSubview:backImg];
    [self.view addSubview:backClick];
    [self.scrollView addSubview:self.buttonView];
}

- (void)backToPage
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (MyScrollerView *)picScrollView
{
    if (!_picScrollView) {
        self.picScrollView = [[MyScrollerView alloc]initWithFrame:CGRectMake(0, -20, self.view.bounds.size.width, ScreenWidth / 3 * 2)];
    }
    return _picScrollView;
}

- (UITableView *)tableView
{
    if (!_tableview) {
        self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, ScreenWidth / 3 * 2 - 20 + 80 * H, self.view.bounds.size.width, 810*H) style:UITableViewStylePlain];
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.scrollEnabled = NO;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [self.scrollView addSubview:_tableview];
        [_tableview registerClass:[NotesTableViewCell class] forCellReuseIdentifier:@"reuse8"];
    }
    return _tableview;
}

- (UIView *)buttonView
{
    if (!_buttonView) {
        self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenWidth / 3 * 2 - 20, self.view.bounds.size.width, 80*H)];
        self.buttonView.backgroundColor = [UIColor colorWithRed:0 green:220.0 / 256.0 blue:220.0 / 256.0 alpha:1.0];
        NSArray *arr = [[NSArray alloc]initWithObjects:@"精选", @"游记", @"美食", nil];
        NSArray *arr1 = [[NSArray alloc]initWithObjects:@"fan-youji.png", @"fan-jingdian.png", @"fan-meishi.png", nil];
        for (int i = 0;i < 3; i ++) {
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((125*i)*W, 10*H, 125*W, 80*H)];
            UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:arr1[i]]];
            imgView.frame = CGRectMake(47*W, 0, 30*W, 30*H);
            [button addSubview:imgView];
            button.tag = 12306+i;
            [button addTarget:self action:@selector(buttonWithBtn:) forControlEvents:UIControlEventTouchUpInside];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(40*W, 40*H, 45*W, 20*H)];
            label.font = [UIFont systemFontOfSize:14];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor= [UIColor whiteColor];
            [button addSubview:label];
            label.text = arr[i];
            [self.buttonView addSubview:button];
        }
    }
    return _buttonView;
}

- (NSMutableArray *)photoArr
{
    if (!_photoArr) {
        self.photoArr = [NSMutableArray array];
    }
    return _photoArr;
}

- (NSMutableArray *)arr
{
    if (!_arr) {
        _arr = [NSMutableArray array];
    }
    return _arr;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 1060 * H);
    }
    return _scrollView;
}

- (void)handle
{
    self.dataArr = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat: @"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.87981972226579&lon=121.5419585079168&page=1&track_app_channel=App Store&track_app_version=6.8&track_device_info=iPhone7,2&track_deviceid=357AAE6A-2231-4843-84E7-0ED164F35BDD&track_os=ios 9.0.2&v=1",self.cityID];
    [HandleData handleDataWithUrl:str completion:^(id data) {
        self.dic = data;
        NSDictionary *dic = [self.dic objectForKey:@"data"];
        NSArray *arr = [dic objectForKey:@"photos"];
        for (NSString *str in arr) {
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
            UIImage *image = [[UIImage alloc]initWithData:data];
            [self.dataArr addObject:image];
        }
        [self.picScrollView getImageArr:self.dataArr];
        NSMutableArray *arr1 = [dic objectForKey:@"hot_mguide"];
        for (NSMutableDictionary *dic1 in arr1) {
            NotesModel *model = [NotesModel modelWithDic:dic1];
            [self.arr addObject:model];
        }
        [self.tableView reloadData];
    }];
}

- (void)buttonWithBtn:(UIButton *)btn
{
    if (btn.tag == 12306) {
        NotesViewController *view = [[NotesViewController alloc]init];
        view.ID = self.cityID;
        [self.navigationController pushViewController:view animated:YES];
    }else{
        FoodListViewController *food = [[FoodListViewController alloc]init];
        food.cityID = self.cityID;
        food.num = btn.tag - 12306;
        [self.navigationController pushViewController:food animated:YES];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse8"];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    NotesModel *model = self.arr[indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"chartlet_image_traning@2x.png"]];
    cell.foot.image = [UIImage imageNamed:@"image.png"];
    cell.titleLabel.text = model.title;
    [cell.userView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"chartlet_image_traning@2x.png"]];
    [cell.countView removeFromSuperview];
    cell.countLabel.text = model.count;
    cell.titleLabel.text = model.title;
    cell.username.text = model.username;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 270 * H;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesModel *model = self.arr[indexPath.row];
    NotesDetailViewController *view = [[NotesDetailViewController alloc]init];
    view.ID = model.ID;
    [self.navigationController pushViewController:view animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
